package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.app.dao.MemberPurseDao;
import io.renren.modules.app.entity.MemberPurseEntity;
import io.renren.modules.app.service.MemberPurseService;
import org.springframework.transaction.annotation.Transactional;


@Service("memberPurseService")
public class MemberPurseServiceImpl extends ServiceImpl<MemberPurseDao, MemberPurseEntity> implements MemberPurseService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberPurseEntity> page = this.page(
                new Query<MemberPurseEntity>().getPage(params),
                new QueryWrapper<MemberPurseEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public Integer addMemberPurse(MemberPurseEntity purse) {
        purse.setCreateTime(new Date());
        return baseMapper.insert(purse);
    }

    @Transactional
    @Override
    public Integer editMemberPurse(MemberPurseEntity purse) {
        UpdateWrapper<MemberPurseEntity> wrapper = new UpdateWrapper<>();
        if (purse.getUserId() != null) {
            wrapper.eq("user_id", purse.getUserId());
        }
        return baseMapper.update(purse,wrapper);
    }

    @Override
    public MemberPurseEntity queryUserPurse(Long userId) {
        QueryWrapper<MemberPurseEntity> wrapper = new QueryWrapper<>();
        if (userId != null) {
            wrapper.eq("user_id", userId);
        }
        wrapper.last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

}